﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Perovich.GameObjects.SilverlightWithXna;
using Perovich.GameObjects.Components.TwoDimensional.Particles;
using Perovich.GameObjects.Components.TwoDimensional;
using Perovich.GameObjects.Components.Helpers;
using Perovich.GameObjects.Components.Controls.GameStates;
using Perovich.GameObjects.Components.Controls;

namespace SlXnaApp1
{
    public partial class GamePage : ObjectGamePage
    {
        Button b1;
        Button b2;
        Button b3;
        Button b4;
        Explosion exp;
        SmokeRing ring;
        BoundedObject2D obj1, obj2;

        BoundedObject2D[] objs = new BoundedObject2D[20];

        BoundedObject2D obj3, obj4;

        public GamePage()
        {
            InitializeComponent();
        }

        protected override void Initialize()
        {
            GameStateManager.MakeState("State1",this);
            GameStateManager.MakeState("State2", this);
            GameStateManager.MakeState("CollisionTest", this);

            GameStateManager.States["State1"].InitState(s =>
            {
                b1 = new Button(s);
                b1.Depth = 0;
                b2 = new Button(s);
                b2.Depth = 0;
                b2.Text = "Swap!!";
                b2.Margin = new Thickness(10);
                b2.Border = new Thickness(10);
                b2.ForegroundColor = Color.Black;
                b2.BackgroundColor = Color.Red;
                b2.BorderColor = Color.Blue;
                b2.Clicked += new EventHandler(b2_Clicked);
                obj1 = new BoundedObject2D(s);
                obj2 = new BoundedObject2D(s);
            });


            GameStateManager.States["State2"].InitState(s =>
            {
                b3 = new Button(s);
                b3.Text = "Swap!!";
                b3.Margin = new Thickness(10);
                b3.Border = new Thickness(10);
                b3.ForegroundColor = Color.Black;
                b3.BackgroundColor = Color.Red;
                b3.BorderColor = Color.Blue;
                b3.Clicked += new EventHandler(b3_Clicked);
                b3.Depth = 0;
                for (int i = 0; i < 20; ++i)
                {
                    objs[i] = new BoundedObject2D(s);
                    objs[i].Position = new Vector2(62 + 124 * (i % 4), 62 + 124 * (i / 4));
                    objs[i].Velocity = new Vector2(140, 140);
                    objs[i].Depth = 0.1f;
                }
            });


            GameStateManager.States["CollisionTest"].InitState(s =>
            {
                b4 = new Button(s);
                b4.Text = "Swap!!";
                b4.Margin = new Thickness(10);
                b4.Border = new Thickness(10);
                b4.ForegroundColor = Color.Black;
                b4.BackgroundColor = Color.Red;
                b4.BorderColor = Color.Blue;
                b4.Clicked += new EventHandler(b4_Clicked);
                b4.Depth = 0;
                obj3 = new BoundedObject2D(s);
                obj3.Position = new Vector2(100, 200);
                obj3.Velocity = new Vector2(25, 100);
                obj3.Depth = 0.1f;
                obj4 = new BoundedObject2D(s);
                obj4.Position = new Vector2(300, 200);
                obj4.Velocity = new Vector2(-25, 100);
                obj4.Depth = 0.1f;
                obj3.IsPreciseCollisions = true;
                obj4.IsPreciseCollisions = true;
                obj3.AddCollisionTargets(obj4);
                obj3.Collided += new EventHandler<Perovich.GameObjects.Components.CollisionEventArgs>(InteractiveObject2D.DefaultBounceHandler);
                s.BackgroundColor = Color.Blue;
            });

            ring = new SmokeRing(this, 2);
            exp = new Explosion(this, 1);
           


            b1.Position = new Vector2(100, 100);
            b1.Text = "Hai!!";
            b1.Margin = new Thickness(5);
            b1.Border = new Thickness(10);
            b1.ForegroundColor = Color.Black;
            b1.BackgroundColor = Color.Red;
            b1.BorderColor = Color.Blue;
            b1.Clicked += new EventHandler(b1_Clicked);


            exp.Position = new Vector2(0, 0);
            exp.Width = Graphics.GraphicsDevice.Viewport.Width;
            exp.Height = Graphics.GraphicsDevice.Viewport.Height;
            ring.Position = new Vector2(0, 0);
            ring.Width = Graphics.GraphicsDevice.Viewport.Width;
            ring.Height = Graphics.GraphicsDevice.Viewport.Height;


            obj1.Position = new Vector2(100, 400);
            obj2.Position = new Vector2(300, 400);
            obj1.Velocity = new Vector2(80, 80);
            obj2.Velocity = new Vector2(-80, 80);
            obj1.AddCollisionTargets(obj2);
            obj1.Collided +=new EventHandler<Perovich.GameObjects.Components.CollisionEventArgs>(InteractiveObject2D.DefaultBounceHandler);
            obj1.Depth = 0.1f;
            obj2.Depth = 0.1f;


            foreach (var obj in objs)
            {
                obj.AddCollisionTargets(objs);
                obj.Collided += new EventHandler<Perovich.GameObjects.Components.CollisionEventArgs>(InteractiveObject2D.DefaultBounceHandler);
            }

            GameStateManager.SetActiveState("CollisionTest");

            base.Initialize();
        }

        void b4_Clicked(object sender, EventArgs e)
        {
            GameStateManager.SetActiveState("State1");
        }
        void b3_Clicked(object sender, EventArgs e)
        {
            GameStateManager.SetActiveState("CollisionTest");
        }

        void b2_Clicked(object sender, EventArgs e)
        {
            GameStateManager.SetActiveState("State2");
        }

        protected override void LoadContent()
        {
            


            obj1.Texture = Content.Load<Texture2D>("PhoneGameThumb");
            obj2.Texture = Content.Load<Texture2D>("PhoneGameThumb");
            obj1.Origin = new Vector2(obj1.Texture.Width / 2, obj1.Texture.Height / 2);
            obj2.Origin = new Vector2(obj2.Texture.Width / 2, obj2.Texture.Height / 2);

            obj1.Bounds = new BoundingBox(new Vector3(0, 0, 0),
                                    new Vector3(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0));
            obj2.Bounds = new BoundingBox(new Vector3(0, 0, 0),
                                    new Vector3(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0));
            obj3.Texture = Content.Load<Texture2D>("CollisionTestTex");
            obj4.Texture = Content.Load<Texture2D>("CollisionTestTex");
            obj3.Origin = new Vector2(obj3.Texture.Width / 2, obj3.Texture.Height / 2);
            obj4.Origin = new Vector2(obj4.Texture.Width / 2, obj4.Texture.Height / 2);

            obj3.Bounds = new BoundingBox(new Vector3(0, 0, 0),
                                    new Vector3(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0));
            obj4.Bounds = new BoundingBox(new Vector3(0, 0, 0),
                                    new Vector3(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0));
            base.LoadContent();

            foreach (var obj in objs)
            {
                obj.Texture = Content.Load<Texture2D>("PhoneGameThumb");
                obj.Origin = new Vector2(obj.Texture.Width / 2, obj.Texture.Height / 2);
                obj.Bounds = new BoundingBox(new Vector3(0, 0, 0),
                                    new Vector3(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0));
            }
            b2.Position = new Vector2(GraphicsDevice.Viewport.Width - b2.Font.MeasureString(b2.Text).X - 40,0);
            b3.Position = new Vector2(GraphicsDevice.Viewport.Width - b3.Font.MeasureString(b3.Text).X - 40, 0);
            b4.Position = new Vector2(100, 100);
        }


        void b1_Clicked(object sender, EventArgs e)
        {
            Vector2 where = Vector2.Zero;
            // create the explosion at some random point on the screen.
            where.X = Perovich.GameObjects.Components.ComponentsExtensions.RandomBetween(0, Graphics.GraphicsDevice.Viewport.Width);
            where.Y = Perovich.GameObjects.Components.ComponentsExtensions.RandomBetween(0, Graphics.GraphicsDevice.Viewport.Height);
            exp.AddParticles(where);
            ring.AddParticles(where);
        }



    }
}